package com.freight.tws.controller;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import lombok.RequiredArgsConstructor;
import com.freight.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freight.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freight.common.log.annotation.Log;
import com.freight.common.log.enums.BusinessType;
import com.freight.common.core.web.controller.BaseController;
import com.freight.common.core.web.domain.AjaxResult;
import com.freight.common.core.utils.poi.ExcelUtil;
import com.freight.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.common.core.web.page.Condition;
import com.freight.tws.domain.AppVersion;
import com.freight.tws.service.IAppVersionService;
import com.freight.common.core.web.page.TableDataInfo;

/**
 * app版本管理Controller
 * 
 * @author ruoyi
 * @date 2022-03-04
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/version")
public class AppVersionController extends BaseController
{
    private final IAppVersionService iAppVersionService;

    /**
     * 查询app版本管理列表
     */
    @PreAuthorize(hasPermi="tws:version:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, AppVersion appVersion)
    {
        LambdaQueryWrapper<AppVersion> lqw = new LambdaQueryWrapper<AppVersion>();
        if (StringUtils.isNotBlank(appVersion.getType())) {
            lqw.eq(AppVersion::getType, appVersion.getType());
        }
        if (StringUtils.isNotBlank(appVersion.getFileUrl())) {
            lqw.eq(AppVersion::getFileUrl, appVersion.getFileUrl());
        }
        if (StringUtils.isNotBlank(appVersion.getVersion())) {
            lqw.eq(AppVersion::getVersion, appVersion.getVersion());
        }
        if (appVersion.getVersionNo() != null) {
            lqw.eq(AppVersion::getVersionNo, appVersion.getVersionNo());
        }
        if (StringUtils.isNotBlank(appVersion.getIsMust())) {
            lqw.eq(AppVersion::getIsMust, appVersion.getIsMust());
        }
        lqw.orderByDesc(AppVersion::getCreateTime);
        IPage<AppVersion> list = iAppVersionService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 查询 app最新版本号
     */
    @GetMapping("/getNewVersionCode")
    public AjaxResult getNewVersionCode() {
        List<AppVersion> androids = iAppVersionService.lambdaQuery().eq(AppVersion::getType, "android").list();
        AppVersion android = androids.stream().max(Comparator.comparing(AppVersion::getVersionNo)).orElse(null);
        List<AppVersion> ioss = iAppVersionService.lambdaQuery().eq(AppVersion::getType, "ios").list();
        AppVersion ios = ioss.stream().max(Comparator.comparing(AppVersion::getVersionNo)).orElse(null);
        Map<String, Long> map = new HashMap<>();
        map.put("android", StringUtils.isNotNull(android) ? android.getVersionNo() : 0L);
        map.put("ios", StringUtils.isNotNull(ios) ? ios.getVersionNo() : 0L);
        return AjaxResult.success(map);
    }

    /**
     * 导出app版本管理列表
     */
    @PreAuthorize(hasPermi = "tws:version:export")
    @Log(title = "app版本管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppVersion appVersion) throws IOException
    {
        LambdaQueryWrapper<AppVersion> lqw = new LambdaQueryWrapper<AppVersion>(appVersion);
        List<AppVersion> list = iAppVersionService.list(lqw);
        ExcelUtil<AppVersion> util = new ExcelUtil<AppVersion>(AppVersion. class);
        util.exportExcel(response, list, "version");
    }

    /**
     * 获取app版本管理详细信息
     */
    @PreAuthorize(hasPermi="tws:version:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iAppVersionService.getById(id));
    }

    /**
     * 新增app版本管理
     */
    @PreAuthorize(hasPermi="tws:version:add")
    @Log(title = "app版本管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppVersion appVersion) {
        return toAjax(iAppVersionService.save(appVersion) ? 1 : 0);
    }

    /**
     * 修改app版本管理
     */
    @PreAuthorize(hasPermi="tws:version:edit")
    @Log(title = "app版本管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppVersion appVersion) {
        return toAjax(iAppVersionService.updateById(appVersion) ? 1 : 0);
    }

    /**
     * 删除app版本管理
     */
    @PreAuthorize(hasPermi="tws:version:remove")
    @Log(title = "app版本管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iAppVersionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
